package org.example.iris.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.example.iris.entity.Company;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
@Mapper
public interface CompanyMapper {

    /**
     * 插入公司信息到 company 表
     * @param company 公司对象
     * @return 插入的记录数
     */
    @Insert("INSERT INTO company (user_id,company_name, description, culture, logo, website, city, examine_passed) " +
            "VALUES (#{userId},#{companyName}, #{description}, #{culture}, #{logo}, #{website}, #{city}, false )") // 默认值 0
    int insert(Company company);

    /**
     * 根据 userId 获取对应的 companyId
     * @param userId 用户ID
     * @return 对应的 companyId
     */
    @Select("SELECT company_id FROM company WHERE user_id = #{userId}")
    Integer getCompanyIdByUserId(Integer userId);

    @Select("select * from company where user_id=#{userId};")
    Company getCompanyByUserId(Integer userId);
    /**
     * 更新公司信息
     * @param company 公司对象，包含更新的字段
     * @return 更新的记录数
     */
    @Update("UPDATE company SET " +
            "company_name = #{companyName}, " +
            "description = #{description}, " +
            "culture = #{culture}, " +
            "logo = #{logo}, " +
            "website = #{website}, " +
            "city = #{city} " +
            "WHERE company_id = #{companyId}")
    int updateCompanyInfo(Company company);

    //通过审核后设置examine_passed为true
    @Update("UPDATE company SET examine_passed = true WHERE company_id = #{companyId}")
    int acceptCompanyRegistration(int companyId);

    @Select("select * from company ORDER BY RAND() LIMIT 6")
    List<Company> recommendCompany();

    @Select("select * from company where company_name like CONCAT('%',#{keywords},'%') or description like CONCAT('%',#{keywords},'%')")
    List<Company> selectCompany(String keywords);

    @Select("select * from company where company_id = #{companyId}")
    Company findById(Integer companyId);

    @Select("select * from company where user_id = #{userId}")
    Company findByUserId(Integer userId);

    @Select("select * from company where examine_passed = true")
    List<Company> allCompany();
}
